package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * index页面跳转
 *
 * @author LaiSanShan
 * @date 2021-08-01
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    /**
     * 主页页面
     *
     * @param model 模型
     * @return classPath:/templates/返回值.html
     */
    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        // 查询页面分类数据
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Categorys();

        model.addAttribute("categorys", categoryEntityList);
        return "index";
    }

    /**
     * 获取页面分类渲染数据
     * @return 渲染数据
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> data = categoryService.getCatalogJson();

        return data;
    }

}
